/**
 * Created by JetBrains PhpStorm.
 * User: ishowshao
 * Date: 11-7-11
 * Time: 上午11:01
 */
DemoViewportUi = Ext.extend(Ext.Viewport, {
    layout: 'border',
    initComponent: function () {
        this.items = [
            {
                xtype: 'container',
                region: 'north',
                itemId: 'north',
                height: 80,
                html: 'Demo App',
                split: true
            },
            {
                xtype: 'panel',
                itemId: 'center',
                region: 'center',
                border: false,
                layout: 'fit',
                items: [
                    {
                        xtype: 'Home'
                    }
                ]
            },
            {
                xtype: 'MenuTreePanel',
                region: 'west',
                split: true,
                collapsible: true,
                animCollapse: false,
                collapseMode: 'mini',
                header: false
            }
        ];
        DemoViewportUi.superclass.initComponent.call(this);
        this.addListener('afterrender', this.init, this);
    }
});

DemoViewport = Ext.extend(DemoViewportUi, {
    init: function () {
        this.menuTree = this.get(2);
        this.on('redirect', this.onRedirect, this);
        this.beforePageLoadConfig = {};
    },
    loadPage: function (node) {
        var that = this;
        if (node.attributes.iframe) {
            //iframe页面不用boss检查权限
            node.select();
            this.currentPage = 'iframe';
            this.doLoadPage(node.attributes.url);
        } else {
            that.doLoadPageCallback(node);
        }
    },
    doLoadPageCallback: function (node) {
        this.currentPage = node.id;
        node.select();
        this.doLoadPage();
    },
    doLoadPage: function (url) {
        this.regBeforePageLoad(false);
        this.get('center').removeAll(true);
        this.get('center').add({
            xtype: config.page.xtype[this.currentPage],
            src: url
        });
        this.get('center').doLayout();
    },
    reLoadPage: function () {
        this.doLoadPage();
    },
    /**
     * 注册一个页面切换前的事件
     * 
     * @param {Object} config
     */
    regBeforePageLoad: function (config) {
        this.beforePageLoadConfig = config || {};
    },
    getCenter: function () {
        return this.get('center');
    },
    headerMessage: function () {
        var that = this;
        Ext.Ajax.request({
            url: config.common.headerMessage,
            success: function (response) {
                var result = Ext.decode(response.responseText);
                if (result.success) {
                    var data = result.data;
                    var msg = document.createElement('span');
                    msg.className = 'header-message';
                    msg.innerHTML = String.format('<span class="color-white">欢迎您，{1}</span>&nbsp;&nbsp;&nbsp;&nbsp;<a href="{0}">退出</a>&nbsp;|&nbsp;<span class="boss1"><a href="{2}">进入BOSS1.0</a></span>', data['logoutUrl'], data['message'], data['boss1Url']);
                    that.get('north').getEl().select('div.header').appendChild(msg);
                }
            },
            failure: function (response) {
                alert(config.errorMsg.networkFail + response.status);
            }
        });
    },
    /**
     * 处理页面转向请求
     *
     * @param {String} type 页面类型 page or iframe
     * @param {String} page 转向页面的xtype or url
     * @param {Object} args 传递给转向页面的参数
     */
    onRedirect: function (type, page, args) {
        this.regBeforePageLoad(false);
        this.get('center').removeAll(true);

        if (type == 'page') {
            page && this.get('center').add({
                xtype: config.page.xtype[page],
                args: args
            });
            this.menuTree.selectNodeById(page);
        } else if (type == 'iframe') {
            this.get('center').add({
                xtype: 'Iframe',
                src: page
            });
        }
        
        this.get('center').doLayout();
    }
});
